package country

import (
	"github.com/jinzhu/gorm"
	"time"
)

//CREATE TABLE `sim_info` (
//  `id` int NOT NULL AUTO_INCREMENT,
//  `country_code` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
//  `phone_code` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
//  `mcc` varchar(3) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
//  `mnc` varchar(3) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
//  `sim_carrier_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
//  `network_operator_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
//  `phone_regex` varchar(4096) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
//  `iccid_regex` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
//  `imsi_regex` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
//  `imei_regex` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
//  `imeisv_regex` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
//  `meid_regex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
//  `create_by` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
//  `update_by` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
//  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
//  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
//  `delete_flag` tinyint(1) NOT NULL DEFAULT '0',
//  PRIMARY KEY (`id`) USING BTREE,
//  UNIQUE KEY `idx_mcc_mnc` (`mcc`,`mnc`) USING BTREE,
//  KEY `idx_country_code` (`country_code`) USING BTREE
//) ENGINE=InnoDB AUTO_INCREMENT=1887 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

type SimInfo struct {
	Id                  int       `json:"id" gorm:"primary_key;column:id"`
	CountryCode         string    `json:"country_code" gorm:"column:country_code"`
	PhoneCode           string    `json:"phone_code" gorm:"column:phone_code"`
	Mcc                 string    `json:"mcc" gorm:"column:mcc"`
	Mnc                 string    `json:"mnc" gorm:"column:mnc"`
	SimCarrierName      string    `json:"sim_carrier_name" gorm:"column:sim_carrier_name"`
	NetworkOperatorName string    `json:"network_operator_name" gorm:"column:network_operator_name"`
	PhoneRegex          string    `json:"phone_regex" gorm:"column:phone_regex"`
	IccidRegex          string    `json:"iccid_regex" gorm:"column:iccid_regex"`
	ImsiRegex           string    `json:"imsi_regex" gorm:"column:imsi_regex"`
	ImeiRegex           string    `json:"imei_regex" gorm:"column:imei_regex"`
	ImeisvRegex         string    `json:"imeisv_regex" gorm:"column:imeisv_regex"`
	MeidRegex           string    `json:"meid_regex" gorm:"column:meid_regex"`
	CreateBy            string    `json:"create_by" gorm:"column:create_by"`
	UpdateBy            string    `json:"update_by" gorm:"column:update_by"`
	CreateTime          time.Time `json:"create_time" gorm:"column:create_time"`
	UpdateTime          time.Time `json:"update_time" gorm:"column:update_time"`
	DeleteFlag          int       `json:"delete_flag" gorm:"column:delete_flag"`
}

func (s *SimInfo) TableName() string {
	return "sim_info"
}
func (s *SimInfo) Create(db *gorm.DB) error {
	return db.Table(s.TableName()).Create(s).Error
}
func (s *SimInfo) Update(db *gorm.DB) error {
	return db.Table(s.TableName()).Where("id = ?", s.Id).Update(s).Error
}
func (s *SimInfo) GetByCountryCode(db *gorm.DB) ([]SimInfo, error) {
	var simInfo []SimInfo
	err := db.Table(s.TableName()).Where("country_code = ?", s.CountryCode).Find(&simInfo).Error
	return simInfo, err
}
